using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SmallBusinessKit.DataAccess.Interfaces;

namespace SmallBusinessKit.DataAccess.Infrastructure
{
    /// <summary>
    /// This is the ten-minute implementation of an XML Repository...lol
    /// </summary>
    public abstract class XmlRepositoryBase<T> : IRepository<T> where T : class
    {
        #region Properties

        protected readonly List<T>    DataList;

        #endregion

        #region Methods

        protected XmlRepositoryBase(IList<T> data)
        {
            DataList = (List<T>)data;
        }

        public virtual IQueryable<T> GetAsQueryable()
        {
            var q = DataList.AsQueryable();

            return (q);
        }

        public virtual T GetById(string id)
        {
            throw new NotImplementedException();
        }

        public virtual T Get(Expression<Func<T, bool>> clause)
        {
            var condition = clause.Compile();

            return (DataList.FirstOrDefault(condition));
        }

        public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> clause)
        {
            var condition = clause.Compile();

            return (DataList.Where(condition));
        }

        public virtual IEnumerable<T> GetAll()
        {
            return (DataList);
        }

        #endregion
    }
 }
